package model;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class BasicBudget extends Budget{

	public BasicBudget(String department, String file) {
		super(file);
		this.setDepartment(department);
		loadData(file);
	}
	
	private void loadData(String file){
		try {
			Scanner scanner = new Scanner(new FileInputStream(file));
			String line = scanner.nextLine();
			String[] atts = line.split(",");
			
			this.setBudget(Double.parseDouble(atts[0]));
			this.setExpenses(Double.parseDouble(atts[1]));
			this.setProfit(Double.parseDouble(atts[2]));
			this.setTotal(Double.parseDouble(atts[3]));
			this.setNumberOfEmployees(Integer.parseInt(atts[4]));
			this.setTotalSalary(Double.parseDouble(atts[5]));
			
			String desc = "";
			
			while(scanner.hasNext()){
				desc += scanner.nextLine() + "\n";
			}
			
			this.setDescription(desc);
			
			scanner.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void showInfo(Container parent){
		BasicDialog s = new BasicDialog(parent);
		s.setVisible(true);
	}
	
	private class BasicDialog extends JDialog{
		Container parent;
		
		public BasicDialog(Container parent){
			this.parent = parent;
			initGUI();
		}
		
		private void initGUI(){
			JPanel panel = new JPanel(new BorderLayout());
			
			String desc = getDescription();
			desc = "<html><pre>" + desc;
			desc = desc + "</html></pre>";
			desc = desc.replace("\t","&nbsp;&nbsp;&nbsp;");
			JLabel text = new JLabel(desc);
			
			panel.add(text, BorderLayout.CENTER);
			panel.add(new JButton(new ButtonAction("Close")),BorderLayout.SOUTH);
			panel.add(new JPanel(),BorderLayout.WEST);
			panel.add(new JPanel(),BorderLayout.EAST);
			this.add(panel);
			
			
			
			this.pack();
			this.setModal(true);
			int w = getWidth();
			int h = getHeight();
			Dimension screenSize = parent.getSize();
			int screenWidth = screenSize.width;
			int screenHeight = screenSize.height;
			int x = (screenWidth - w)/2;
			int y = (screenHeight - h)/2 + 150;
			setLocation(x,y);
		}
		
		private class ButtonAction extends AbstractAction{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public ButtonAction(String name) {
				putValue(Action.NAME, name); 
			}
			
			public void actionPerformed(ActionEvent event) {
				if(this.getValue(Action.NAME).equals("Close")){
					BasicDialog.this.setVisible(false);
				}
			}
		}
	}
	
}
